****Analyses for Party Unity Perceptions Paper Submission 

*Date: 4/28/2016

***Use for data formatting and setup. 

*Dataset: CCES14_WES_OUTPUT_Aug2015.dta. 

*This dataset can be downloaded from 
*Fowler, Erika, 2017, "CCES 2014, Team Module of Wesleyan University (WES)", 
*doi:10.7910/DVN/NIU9IT, Harvard Dataverse, V1, UNF:6:zSpyj3f8bS+IeBj7472X2w==


*****Basic Variable Coding 

*Gender
tab gender

*Race
tab race

gen white = 1 if race==1
replace white = 0 if race !=1 & race !=.

*Education 
tab educ

*Income 
tab faminc
tab faminc, nolab
replace faminc = 14 if faminc==32

gen inc = faminc
replace inc = 0 if faminc>16 & faminc!=.
tab inc

gen inc_dk = 1 if inc==0 
replace inc_dk = 0 if inc !=0 & inc !=.
tab inc inc_dk

*Age 

tab birthyr

gen age = .
replace age = 2014-birthyr

tab age

*Party ID

tab pid7
tab pid7, nolab
gen pid7b = pid7 if pid7<8

gen pid3b = 1 if pid7>=1 & pid7 <4
replace pid3b=2 if pid7==4
replace pid3b=3 if pid7>4 & pid7 <=7
tab pid3b
label define pid3b 1 "Democrat" 2 "Independent" 3 "Republican" 
label values pid3b pid3b 

tab pid3b pid7

*PID strength

gen pidstrength=pid7b
replace pidstrength=. if pidstrength>7
replace pidstrength=abs(4-pidstrength)
tab pidstrength pid7b

*Senator Info 

tab1 CurrentSen1Name CurrentSen2Name

tab1 CurrentSen*Party

gen sen1party = 1 if CurrentSen1Party=="Democratic"
replace sen1party = 2 if CurrentSen1Party=="Republican" 

gen sen2party = 1 if CurrentSen2Party=="Democratic" 
replace sen2party = 2 if CurrentSen2Party=="Republican" 
replace sen2party = 3 if CurrentSen2Party=="Independent"
replace sen2party = 1 if sen2party==3
tab sen2party

label define senparty 1 "Democrat" 2 "Republican" 3 "Independent" 
label values sen1party senparty 
label values sen2party senparty

tab sen1party 
tab sen2party

*Senator-Voter Party Agreement

gen sen1ptyagree = 1 if pid3b == 1 & sen1party==1
replace sen1ptyagree = 1 if pid3b == 3 & sen1party==2
replace sen1ptyagree = 0 if pid3b == 1 & sen1party==2 
replace sen1ptyagree = 0 if pid3b == 3 & sen1party==1 
replace sen1ptyagree = 2 if pid3b==2

label define senptyagree  0 "Disagree" 1 "Agree" 2 "Independent"
label values sen1ptyagree senptyagree

tab sen1ptyagree

gen sen2ptyagree = 1 if pid3b == 1 & sen2party==1
replace sen2ptyagree = 1 if pid3b == 3 & sen2party==2
replace sen2ptyagree = 0 if pid3b == 1 & sen2party==2 
replace sen2ptyagree = 0 if pid3b == 3 & sen2party==1 
replace sen2ptyagree = 2 if pid3b==2 

label values sen2ptyagree senptyagree

tab sen2ptyagree

****Coding Perceptions of Party Unity 

describe WES304
describe WES305
describe WES306

*Step 1 - Create String Variables with Value Labels as entries
tab WES304
decode WES304, gen(WES304_value)
decode WES305, gen(WES305_value)
decode WES306, gen(WES306_value)
decode WES307, gen(WES307_value)
decode WES308, gen(WES308_value)
decode WES309, gen(WES309_value)

*Step 2 - Create numeric variables

gen WES304_final = real(WES304_value)
gen WES305_final = real(WES305_value)
gen WES306_final = real(WES306_value)
gen WES307_final = real(WES307_value)
gen WES308_final = real(WES308_value)
gen WES309_final = real(WES309_value)

tab WES309_final

*Step 3 - Create Combined Variable

tab WES304_309_Treat
gen treat = WES304_309_Treat
tab treat
label define treat 1 "Polarized Cue" 2 "Moderate Cue" 3 "No Cue" 
label values treat treat
tab treat

gen perception_unitySen1 = . 
replace perception_unitySen1 = WES304_final if treat==1 
replace perception_unitySen1 = WES306_final if treat==2 
replace perception_unitySen1 = WES308_final if treat==3 

gen perception_unitySen1DK = 1 if WES304_value == "Don't know" & treat==1
replace perception_unitySen1DK = 1 if WES306_value == "Don't know" & treat==2
replace perception_unitySen1DK = 1 if WES308_value == "Don't know" & treat==3
tab perception_unitySen1DK
replace perception_unitySen1DK = 0 if perception_unitySen1 !=.
tab perception_unitySen1DK

gen perception_unitySen2 = . 
replace perception_unitySen2 = WES305_final if treat==1 
replace perception_unitySen2 = WES307_final if treat==2 
replace perception_unitySen2 = WES309_final if treat==3 

gen perception_unitySen2DK = 1 if WES305_value == "Don't know" & treat==1
replace perception_unitySen2DK = 1 if WES307_value == "Don't know" & treat==2
replace perception_unitySen2DK = 1 if WES309_value == "Don't know" & treat==3
tab perception_unitySen2DK
replace perception_unitySen2DK = 0 if perception_unitySen2 !=.
tab perception_unitySen2DK

tab treat, sum(perception_unitySen1)
tab treat, sum(perception_unitySen2)

******************
****Stacking Data 
******************

gen id=_n
sum id

*Step 1 - Senator Name. Need to convert to string. It is already a string

tab CurrentSen1Name
tab CurrentSen2Name

rename CurrentSen1Name CurrentSenName1 
rename CurrentSen2Name CurrentSenName2
rename sen1party senparty1 
rename sen2party senparty2
rename sen1ptyagree senptyagree1 
rename sen2ptyagree senptyagree2 
rename perception_unitySen1DK perception_unitySenDK1
rename perception_unitySen2DK perception_unitySenDK2
rename perception_unitySen1 perception_unitySen1
rename perception_unitySen2 perception_unitySen2

*Step 2 - Reshape Command 

reshape long ///
CurrentSenName senparty senptyagree perception_unitySenDK perception_unitySen ///
perception_unitySenPost, i(id) j(sennum)

tab CurrentSenName
rename CurrentSenName senname

*Step 3 - Generate Senator-Specific Variables

*Numerical Senator ID
encode senname, gen(senid)
tab senid, nolab

*Combined Unity Measure
gen perceivedunity = perception_unitySen

tab perceivedunity

*Party Agreement 

gen ptyagree = senptyagree 

tab ptyagree, gen(ptyAgreeDum)
rename ptyAgreeDum1  ptyDis
rename ptyAgreeDum2  ptyAg
rename ptyAgreeDum3  independent 

tab ptyagree

*Political Knowledge

tab CC14_309a
gen know1 = 1 if CC14_309a==1
replace know1 = 0 if CC14_309a !=1 & CC14_309a!=.
tab know1

tab CC14_309b
gen know2 = 1 if CC14_309b==2 
replace know2 = 0 if know2==. 
replace know2 = . if CC14_309b==.
tab know2 know1

tab CC14_310b
tab CurrentSen1Party
tab CC14_310b CurrentSen1Party if sennum==1

gen know3 = . 
replace know3 = 1 if CC14_310b == 2 & CurrentSen1Party=="Republican"
replace know3 = 1 if CC14_310b == 3 & CurrentSen1Party=="Democratic"
replace know3 = 0 if know3 ==. 
replace know3 = . if CC14_310b==. 
replace know3 = . if CurrentSen1Party=="" 
tab know3

tab CC14_310c CurrentSen2Party if sennum==2
tab CurrentSen2Party

gen know4 = . 
replace know4 = 1 if CC14_310c == 2 & CurrentSen2Party == "Republican"
replace know4 = 1 if CC14_310c == 3 & CurrentSen2Party == "Democratic"
replace know4 = 1 if CC14_310c == 3 & CurrentSen2Party == "Independent"
replace know4 = 0 if know4!=1 
replace know4 = . if CC14_310c==. 
replace know4 = . if CurrentSen2Party=="" 
tab know4

*Governor 
tab CC14_310a
tab CurrentGovParty

gen knowgov = 1 if CC14_310a == 2 & CurrentGovParty=="Republican" 
replace knowgov = 1 if CC14_310a == 3 & CurrentGovParty=="Democratic" 
replace knowgov = 0 if knowgov==. 
tab knowgov

*House Member 
tab CC14_310d
tab CurrentHouseParty

gen knowhouse = 1 if CC14_310d == 2 & CurrentHouseParty=="Republican" 
replace knowhouse = 1 if CC14_310d == 3 & CurrentHouseParty=="Democratic" 
replace knowhouse = 0 if knowhouse==. 
tab knowhouse

/*Americans for Prosperity Lean*/
tab WES225
tab WES225, nolabel
gen know5=WES225
replace know5=0 if WES225!=2
replace know5=1 if know5==2
tab know5

/*League of Conservation Voters Lean*/
tab WES229
tab WES229, nolabel
gen know6=WES229
replace know6=0 if know6!=1
tab know6

/*NRA Lean*/
tab WES230
tab WES230, nolabel
gen know7=WES230
replace know7=0 if know7!=2
replace know7=1 if know7==2
tab know7

/*Planned Parenthood Lean*/
tab WES228
tab WES228, nolabel
gen know8=WES228
replace know8=0 if know8!=1
tab know8

*Overall Scale 
gen knowall = know1+know2+know3+know4+know5+know6+know7+know8+knowhouse+knowgov
sum knowall
tab knowall
hist knowall,d

*Main CCES Only Scale
gen knowall_CCES = know1+know2+know3+know4+knowhouse+knowgov
tab knowall_CCES
hist knowall_CCES,d

sum knowall knowall_CCES
pwcorr knowall knowall_CCES

**********************************************
*Coding ICPSR Numbers for Senators - Using String Variable 
**********************************************

tab inputstate
sort inputstate
tab senname

gen icpsr = . 
replace icpsr = 40700 if senname == "Amy Klobuchar"
replace icpsr = 40904 if senname == "Al Franken" 
replace icpsr = 41300 if senname == "Angus King" 
replace icpsr = 14440 if senname == "Barbara A. Mikulski"
replace icpsr = 15011 if senname == "Barbara Boxer"
replace icpsr = 15408 if senname == "Benjamin L. Cardin" 
replace icpsr = 29147 if senname == "Bernard 'Bernie' Sanders"
replace icpsr = 14651 if senname == "Bill Nelson"
replace icpsr = 40705 if senname == "Bob Corker"
replace icpsr = 14709 if senname == "Carl Levin"
replace icpsr = 14858 if senname == "Charles E. Schumer"
replace icpsr = 40916 if senname == "Christopher A. Coons"
replace icpsr = 20707 if senname == "Christopher Murphy" 
replace icpsr = 14226 if senname == "Chuck Grassley"
replace icpsr = 40701 if senname == "Claire McCaskill"
replace icpsr = 41308 if senname == "Cory Booker"
replace icpsr = 14806 if senname == "Dan Coats"
replace icpsr = 29918 if senname == "David Vitter"
replace icpsr = 20730 if senname == "Dean Heller"
replace icpsr = 41302 if senname == "Deb Fischer"
replace icpsr = 29732 if senname == "Debbie Stabenow"
replace icpsr = 49300 if senname == "Dianne Feinstein"
replace icpsr = 14435 if senname == "Edward Markey"
replace icpsr = 41301 if senname == "Elizabeth Warren"
replace icpsr = 15054 if senname == "Harry Reid"
replace icpsr = 41303 if senname == "Heidi Heitkamp"
replace icpsr = 29142 if senname == "Jack Reed"
replace icpsr = 40902 if senname == "James E. Risch"
replace icpsr = 15424 if senname == "James M. Inhofe"
replace icpsr = 40906 if senname == "Jeanne Shaheen"
replace icpsr = 20100 if senname == "Jeff Flake"
replace icpsr = 40908 if senname == "Jeff Merkley"
replace icpsr = 49700 if senname == "Jeff Sessions"
replace icpsr = 29722 if senname == "Jerry Moran"
replace icpsr = 20717 if senname == "Joe Donnelly"
replace icpsr = 40915 if senname == "Joe Manchin III"
replace icpsr = 20101 if senname == "John Boozman"
replace icpsr = 40305 if senname == "John Cornyn"
replace icpsr = 14922 if senname == "John D. Rockefeller IV"
replace icpsr = 41107 if senname == "John Hoeven"
replace icpsr = 15039 if senname == "John McCain"
replace icpsr = 29754 if senname == "John Thune"
*replace icpsr =  if senname == "John Walsh"
replace icpsr = 29909 if senname == "Johnny Isakson"
replace icpsr = 40702  if senname == "Jon Tester"
replace icpsr = 40907 if senname == "Kay Hagan"
replace icpsr = 41106 if senname == "Kelly Ayotte"
replace icpsr = 20735 if senname == "Kirsten E. Gillibrand"
replace icpsr = 40304 if senname == "Lamar Alexander"
replace icpsr = 29566 if senname == "Lindsey Graham"
replace icpsr = 40300 if senname == "Lisa Murkowski"
replace icpsr = 41102 if senname == "Marco Rubio"
replace icpsr = 39310 if senname == "Maria Cantwell"
replace icpsr = 40900 if senname == "Mark Begich"
replace icpsr = 20115 if senname == "Mark Kirk"
replace icpsr = 40301 if senname == "Mark L. Pryor"
replace icpsr = 40909 if senname == "Mark R. Warner"
replace icpsr = 29906 if senname == "Mark Udall"
replace icpsr = 20930 if senname == "Martin Heinrich"
replace icpsr = 49702 if senname == "Mary Landrieu"
replace icpsr = 40910 if senname == "Michael F. Bennet"
replace icpsr = 29345 if senname == "Mike Crapo"
replace icpsr = 40905 if senname == "Mike Johanns"
replace icpsr = 41110 if senname == "Mike Lee"
replace icpsr = 14921 if senname == "Mitch McConnell"
replace icpsr = 14503 if senname == "Orrin Hatch"
replace icpsr = 14852 if senname == "Pat Roberts"
replace icpsr = 29935 if senname == "Pat Toomey"
replace icpsr = 14307 if senname == "Patrick Leahy"
replace icpsr = 49308 if senname == "Patty Murray"
replace icpsr = 41104 if senname == "Rand Paul"
replace icpsr = 41101 if senname == "Richard Blumenthal"
replace icpsr = 29548 if senname == "Richard Burr"
replace icpsr = 94659 if senname == "Richard C. Shelby" 
replace icpsr = 15021 if senname == "Richard J. Durbin"
replace icpsr = 29386 if senname == "Rob Portman"
replace icpsr = 29373 if senname == "Robert Menendez"
replace icpsr = 40703 if senname == "Robert P. Casey, Jr."
replace icpsr = 29534 if senname == "Roger F. Wicker"
replace icpsr = 41111 if senname == "Ron Johnson"
replace icpsr = 14871 if senname == "Ron Wyden"
replace icpsr = 29735 if senname == "Roy Blunt"
replace icpsr = 29512 if senname == "Saxby Chambliss"
replace icpsr = 40704 if senname == "Sheldon Whitehouse"
replace icpsr = 29389 if senname == "Sherrod Brown"
replace icpsr = 49703 if senname == "Susan M. Collins"
replace icpsr = 29940 if senname == "Tammy Baldwin"
replace icpsr = 41304 if senname == "Ted Cruz"
replace icpsr = 14009 if senname == "Thad Cochran"
replace icpsr = 15015 if senname == "Thomas R. Carper"
replace icpsr = 15425 if senname == "Tim Johnson"
replace icpsr = 41305 if senname == "Tim Kaine"
replace icpsr = 21173 if senname == "Tim Scott"
replace icpsr = 29555 if senname == "Tom Coburn"
replace icpsr = 14230 if senname == "Tom Harkin"
replace icpsr = 29924 if senname == "Tom Udall"

tab icpsr

*Max Bacus - 14203. Not in data. 

*Merge in Unity 

merge m:1 icpsr using partyunity_113
tab senname, sum(punity_113)

keep if _merge==3

*Notes: manually change party unity score for King (from 43 to 93.6)
replace punity_113=93.6 if sen_name=="KING"

***Electoral Competitiveness

gen tossup=0
replace tossup=1 if sen_name=="BEGICH"
replace tossup=1 if sen_name=="PRYOR"
replace tossup=1 if sen_name=="UDALL" & state_name=="COLORAD"
replace tossup=1 if sen_name=="LANDRIEU"
replace tossup=1 if sen_name=="SHAHEEN"
replace tossup=1 if sen_name=="HAGAN"


gen leanlikely=0
replace leanlikely=1 if sen_name=="FRANKEN"
replace leanlikely=1 if sen_name=="MERKLEY"
replace leanlikely=1 if sen_name=="WARNER"
replace leanlikely=1 if sen_name=="ROBERTS"
replace leanlikely=1 if sen_name=="MCCONNELL"

gen safe=0
replace safe=1 if sen_name=="SESSIONS"
replace safe=1 if sen_name=="RISCH"
replace safe=1 if sen_name=="COLLINS"
replace safe=0 if sen_name=="JOHANNS"
replace safe=1 if sen_name=="INHOFE"
replace safe=1 if sen_name=="GRAHAM"
replace safe=1 if sen_name=="SCOTT"
replace safe=1 if sen_name=="ALEXANDER"
replace safe=1 if sen_name=="CORNYN"
replace safe=1 if sen_name=="ENZI"
replace safe=1 if sen_name=="COONS"
replace safe=1 if sen_name=="SCHATZ"
replace safe=1 if sen_name=="DURBIN"
replace safe=1 if sen_name=="MARKEY"
replace safe=1 if sen_name=="BOOKER"
replace safe=1 if sen_name=="UDALL" & state_name=="NEW MEX"
replace safe=1 if sen_name=="REED"

gen election = 1 if safe==1 | leanlikely==1 | tossup==1 
replace election = 0 if safe==0 & leanlikely==0 & tossup==0

tab1 tossup leanlikely safe election

**Election Competitiveness

gen elect3=0 if election==0
replace elect3=1 if safe==1
replace elect3=2 if tossup==1
replace elect3=2 if leanlikely==1

tab elect3

save CCES14Stacked.dta, replace



 
